package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.SelectionDormitory;
import com.dj.dormitory.service.SelectionDormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/selectionDormitory")
public class SelectionDormitoryController {
    //根据班级id查询

    @Autowired
    private SelectionDormitoryService selectionDormitoryService;

    @PostMapping("list")
    public Result list(@RequestBody SelectionDormitory selectionDormitory){
        QueryWrapper<SelectionDormitory> wrapper = new QueryWrapper<>();
        wrapper.eq("class_id",selectionDormitory.getClassId());
        wrapper.eq("grade_id",selectionDormitory.getGradeId());
        List<SelectionDormitory> list = selectionDormitoryService.list(wrapper);
        return Result.ok().data("list",list);
    }
    //预选分配
    @PostMapping("add")
    public Result distribute(@RequestBody Map<String,String> map){
        String classId = map.get("classId");
        String gradeId = map.get("gradeId");
        String seletionId = map.get("seletionId");
        String dormitoryIds = map.get("dormitoryIds");
        String[] split = dormitoryIds.split(",");
        List<SelectionDormitory> list = new ArrayList<>();
        for(int i = 0;i<split.length;i++){
            list.add(new SelectionDormitory(Integer.parseInt(split[i]),Integer.parseInt(classId),Integer.parseInt(gradeId),Integer.parseInt(seletionId)));
        }
        selectionDormitoryService.saveBatch(list);
        return Result.ok().message("添加分配宿舍成功");
    }

}

